/**
 * 
 */
package br.com.doclayer.service.cmis;

import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

/**
 * @author helder
 * 
 */
public class DocServiceSessionManager {

  // private static final String REPOSITORY_USER = "admin";
  // private static final String REPOSITORY_PASSWORD = "alfresco123";
  // private static final String REPOSITORY_ATOMPUB_URL =
  // "http://192.168.25.72:8080/alfresco/s/cmis";
  private static final String REPOSITORY_BINDING_TYPE = BindingType.ATOMPUB
      .value();
  // private static final String REPOSITORY_ID =
  // "352cbc34-dd73-4865-b676-48f7e80e3ab9";

  private static Session session;

  private static final Session connect(String username, String password,
      String url, String repositoryId) {
    SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
    Map<String, String> parameter = new HashMap<String, String>();
    parameter.put(SessionParameter.USER, username);
    parameter.put(SessionParameter.PASSWORD, password);
    parameter.put(SessionParameter.ATOMPUB_URL, url);
    parameter.put(SessionParameter.BINDING_TYPE, REPOSITORY_BINDING_TYPE);
    parameter.put(SessionParameter.REPOSITORY_ID, repositoryId);
    return sessionFactory.createSession(parameter);
  }

  public static synchronized Session getSession(String username,
      String password, String url, String repositoryId) {
    if (session == null) {
      session = connect(username, password, url, repositoryId);
    }
    return session;
  }

}
